package view.controller;

import common.Message;
import common.NowUser;
import common.User;
import javafx.event.ActionEvent;
import javafx.fxml.Initializable;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.image.ImageView;
import javafx.scene.input.MouseEvent;
import javafx.stage.Stage;
import server.ClientConnServer;

import javax.swing.*;
import java.io.IOException;
import java.net.URL;
import java.util.ResourceBundle;
import java.util.concurrent.BrokenBarrierException;

public class DeleteFriendController implements Initializable {
    public Button deletetrue;
    public Button deletefalse;
    public Label friendid;
    public ImageView deleteclose;

    public Label getIdfriend() {
        return friendid;
    }
    public void setIdfriend(String idfriend) {
        this.friendid.setText(idfriend);
    }

    @Override
    public void initialize(URL location, ResourceBundle resources) {
        friendid.setText(NowUser.getFriend().getId());
    }

    public void ondeletefalse(ActionEvent actionEvent) {
        Stage stage = (Stage) deletefalse.getScene().getWindow();
        stage.close();
    }

    public void ondeletetrue(ActionEvent actionEvent) throws IOException, BrokenBarrierException, InterruptedException {
        User user = new User();
        NowUser common = new NowUser();
        Message msg = new Message();
        user.setId(common.getUser().getId());
        user.setFriendid(friendid.getText());
        System.out.println("即将被删除好友的id:" + friendid.getText());
        msg.setUser(user);
        msg.setType("deletefriend");
        NowUser.deletefriend=false;
        new ClientConnServer().sendinfo(msg);
        NowUser.cyclicBarrier.reset();
        NowUser.cyclicBarrier.await();
        if(NowUser.deletefriend) {
            System.out.println("删除好友");
            JOptionPane.showMessageDialog(null, "删除好友成功", "提示", JOptionPane.PLAIN_MESSAGE);
            Stage stage = (Stage) deletetrue.getScene().getWindow();
            stage.close();
        } else{
            JOptionPane.showMessageDialog(null, "删除好友失败", "提示", JOptionPane.ERROR_MESSAGE);
        }
    }

    public void todeleteclose(MouseEvent mouseEvent) {
        Stage stage = (Stage) deleteclose.getScene().getWindow();
        stage.close();
    }
}
